package com.kexilo.core.common.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import lombok.Data;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.HashMap;

/**
 * API版本管理配置
 * 
 * @author Kexilo
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "kexilo.api.version")
public class ApiVersionConfig {

    /**
     * 默认API版本
     */
    private String defaultVersion = "v1";

    /**
     * 是否启用版本控制
     */
    private boolean enabled = true;

    /**
     * 版本配置
     */
    private Map<String, VersionInfo> versions = new HashMap<>();

    /**
     * 版本信息
     */
    @Data
    public static class VersionInfo {
        /**
         * 版本状态：active(活跃)、deprecated(弃用)、sunset(下线)
         */
        private String status = "active";

        /**
         * 版本描述
         */
        private String description;

        /**
         * 弃用时间
         */
        private LocalDateTime deprecatedAt;

        /**
         * 下线时间
         */
        private LocalDateTime sunsetAt;

        /**
         * 迁移指引URL
         */
        private String migrationGuide;

        /**
         * 弃用公告
         */
        private String deprecationNotice;
        
        // Getter and Setter methods
        public String getStatus() {
            return status;
        }
        
        public void setStatus(String status) {
            this.status = status;
        }
        
        public String getDescription() {
            return description;
        }
        
        public void setDescription(String description) {
            this.description = description;
        }
        
        public LocalDateTime getDeprecatedAt() {
            return deprecatedAt;
        }
        
        public void setDeprecatedAt(LocalDateTime deprecatedAt) {
            this.deprecatedAt = deprecatedAt;
        }
        
        public LocalDateTime getSunsetAt() {
            return sunsetAt;
        }
        
        public void setSunsetAt(LocalDateTime sunsetAt) {
            this.sunsetAt = sunsetAt;
        }
        
        public String getMigrationGuide() {
            return migrationGuide;
        }
        
        public void setMigrationGuide(String migrationGuide) {
            this.migrationGuide = migrationGuide;
        }
        
        public String getDeprecationNotice() {
            return deprecationNotice;
        }
        
        public void setDeprecationNotice(String deprecationNotice) {
            this.deprecationNotice = deprecationNotice;
        }
    }

    public ApiVersionConfig() {
        // 初始化默认版本配置
        VersionInfo v1 = new VersionInfo();
        v1.setStatus("active");
        v1.setDescription("第一个稳定版本");
        versions.put("v1", v1);
    }

    /**
     * 获取版本信息
     */
    public VersionInfo getVersionInfo(String version) {
        return versions.getOrDefault(version, versions.get(defaultVersion));
    }

    /**
     * 检查版本是否有效
     */
    public boolean isValidVersion(String version) {
        if (!enabled) {
            return true;
        }
        VersionInfo versionInfo = versions.get(version);
        return versionInfo != null && !"sunset".equals(versionInfo.getStatus());
    }

    /**
     * 检查版本是否已弃用
     */
    public boolean isDeprecatedVersion(String version) {
        VersionInfo versionInfo = versions.get(version);
        return versionInfo != null && "deprecated".equals(versionInfo.getStatus());
    }
    
    // Getter methods for ApiVersionConfig
    public boolean isEnabled() {
        return enabled;
    }
    
    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    
    public String getDefaultVersion() {
        return defaultVersion;
    }
    
    public void setDefaultVersion(String defaultVersion) {
        this.defaultVersion = defaultVersion;
    }
    
    public Map<String, VersionInfo> getVersions() {
        return versions;
    }
    
    public void setVersions(Map<String, VersionInfo> versions) {
        this.versions = versions;
    }
}

